﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace DunSilverlight.ViewModel
{
    public class OrganizationModel : ModelBase<OrganizationModel,DunLibrary.Organization.Organization> ,DunLibrary.Core.IItem
    {
        private bool isParentInit = false;
        private OrganizationModel parent;
        public OrganizationModel Parent
        {
            get
            {
                if (!isParentInit && this.parent == null)
                {
                    isParentInit = true;
                    this.parent = new OrganizationModel();
                }
                return this.parent;
            }
            set
            {
                isParentInit = true;
                this.parent = value;
            }
        }

        private bool isChildsInit = false;
        private List<OrganizationModel> childs;
        public List<OrganizationModel> Childs
        {
            get
            {
                if (!isChildsInit && this.childs == null)
                {
                    isChildsInit = true;
                    this.childs = new List<OrganizationModel>();
                }
                return this.childs;
            }
            set
            {
                isChildsInit = true;
                this.childs = value;
            }
        }

        public override DunLibrary.Organization.Organization ToEntity()
        {
            var obj = base.ToEntity();
            if(this.Parent.Id != 0)
                obj.Parent = this.Parent.ToEntity();
            if (this.Childs.Count > 0)
            {
                obj.Childs = new List<DunLibrary.Organization.Organization>();
                foreach (var item in this.Childs)
                {
                    obj.Childs.Add(item.ToEntity());
                }
            }
            return obj;
        }

        /// <summary>
        /// 使用new隐藏父类的static函数
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public new static OrganizationModel Create(DunLibrary.Organization.Organization entity)
        {
            if (entity == null)
                return null;
            OrganizationModel obj = new OrganizationModel();
            obj.Id = entity.Id;
            obj.Name = entity.Name;
            obj.Number = entity.Number;
            obj.GUID = entity.GUID;
            obj.IsUsed = entity.IsUsed;
            obj.Description = entity.Description;

            if(entity.Parent!=null)
                obj.Parent = OrganizationModel.Create(entity.Parent);
            if (entity.Childs != null && entity.Childs.Count>0)
            {
                foreach (var item in entity.Childs)
                {
                    obj.Childs.Add(OrganizationModel.Create(item));
                }
            }

            return obj;
        }
        

        public override bool Validation()
        {
            if (string.IsNullOrEmpty(Name) || Parent.Id==0 )
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
